package com.ll.crud.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ll.crud.bean.Employee;
import com.ll.crud.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * 2020/9/28 - 16:26
 */
@Controller
public class EmployeeController {

    @Autowired
    EmployeeService employeeService;

    /**
     * 查询员工数据
     * @return
     */
    @RequestMapping("/emps")
    public String getEmps(@RequestParam(value = "pageNumber", defaultValue="1") Integer pageNumber, Model module){
        //引入PageHelper分页插件
        //在查询之前只需要调用,传入页码,以及每页的大小
        //默认传入的起始页码是1,每页查询10条数据
        PageHelper.startPage(pageNumber, 10);

        //startPage后面紧跟的这个查询就是一个分页查询
        List<Employee> emps = employeeService.getAll();

        for (Employee employee : emps){
            System.out.println(employee.getEmpName());
        }

        //使用pageInfo保证查询后的结果,只需要将pageInfo交给页面就行了
        //封装了详细的分页信息,包括查询出来的数据
        PageInfo pageInfo = new PageInfo(emps, 5);
        module.addAttribute("pageInfo", pageInfo);

        return "list";
    }
}
